// pages/login/login.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images:["https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/login/%E7%9F%A9%E5%BD%A2%20x0z5SiV%20Copy%201@3x.png","https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/login/%E7%BE%A4%E7%BB%84%2013%20Copy%202@3x.png","https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/login/%E7%BE%A4%E7%BB%84%2014%20Copy%202@3x.png"],
    loadedImages:0,
    ifready:true,
    father:[],
    userinfo:{
      userage:'点击输入生日',
      userrgtime:'注册时间: '+new Date().toISOString().substring(0, 10),
      userid:'点击输入id',
      usergender:'点击输入性别',
    },
    family:{
      familyRelation:'',
      age:'',
    },
    genderchoice:["男","女"],
    relationship:["父亲","母亲","小孩"],
    myHeadurl:'',
  },
  changeHead:function(e){
    var typelist=app.globalData.userList
    const imageList =app.globalData.headUrlStander
    var p=e.currentTarget.dataset.id
    var thisp=typelist[p].userurlid
    //获取实体
    var mid=typelist[p]
    var total=typelist
    thisp+=1;
    if(thisp>=4)
    {
      thisp=0
    }
    var url=imageList[thisp]
    total[p].userurlid=thisp
    total[p].userurl=url
    this.setData({
      myHeadurl: url
    });
    app.globalData.userList=total
  },
  turn1:function(){
    wx.navigateTo({
      url: '/pages/family/family'
    })
  },
  turn2:function(){
    wx.navigateTo({
      url: '/pages/parent/parent'
    })
  },
  turn3:function(){
    wx.navigateTo({
      url: '/pages/child/child'
    })
  },
  turn4:function(){
    wx.navigateTo({
      url: '/pages/toothExam/toothExam'
    })
  },
  getUserProfile(e){
    var that=this;
    var typelist=app.globalData.userList
    wx.showActionSheet({
      alertText:"请选择性别",
      itemList: that.data.genderchoice,
      success(res){
        typelist[0].usergender=that.data.genderchoice[res.tapIndex]
        that.setData({
          father:typelist[0]
        })
      }
    })
  },
  getUserStatus(e){
    var that=this
    var typelist=app.globalData.userList
    wx.showActionSheet({
      alertText:"请选择成员关系",
      itemList: that.data.genderchoice,
      success(res){
        var mid=typelist[0]
        mid.usergender=that.data.genderchoice[res.tapIndex]
        that.setData({
          father:mid
        })
        console.log(this.data.father)
        app.globalData.userList=typelist
      }
    })
  },
  bindDateChange: function (e) {
    var that=this
    var typelist=app.globalData.userList
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var mid=typelist[0]
    mid.userage=e.detail.value
    console.log(mid)
    that.setData({
        father:mid
    })
    app.globalData.userList=typelist

},
close(e) {
  var that=this
  var typelist=app.globalData.userList
  wx.showModal({
    editable:false,//显示输入框
    placeholderText:'输入您的id',//显示输入框提示信息
    success: res => {
      if (res.confirm) { //点击了确认
        typelist[0].userrelation=res.content
        var mid=typelist[0]
        that.setData({
           father:mid
        })
        app.globalData.userList=typelist
      } else {
        console.log('用户点击了取消')
      }
    }
  })
},
  onShow() {
    var mid=app.globalData.userList[0].userurl
    var mid2=app.globalData.userList[0]
    this.setData({
      myHeadurl:mid,
      father:mid2
    })
  },
  onLoad(){
    var that=this
    if(that.data.ifready!='none'){
      let images = this.data.images;
      images.forEach((imgSrc, index) => {
        wx.getImageInfo({
          src: imgSrc,
          success: () => {
            // 图片加载成功
            this.setData({
              loadedImages: this.data.loadedImages + 1
            });
            if (this.data.loadedImages === images.length) {
              if(this.data.ifready==true)
              {
                setTimeout(() => {
                  that.setData({
                    ifready:'none'
                  })
                }, 500);
              }
            }
          },
          fail: () => {
            // 图片加载失败
            console.log(`图片${index + 1}加载失败`);
          }
        });
      });
    }
  },
  initiatePayment: function () {
    console.log("点击支付");
    console.log(app.globalData.usercode)
    const that = this;
    wx.request({
      url: 'https://www.aistomatology.cn/wx/getPayParams',
      method: 'POST',
      header:{  
        'content-type':'application/x-www-form-urlencoded'
      },
      data: {
        code: app.globalData.usercode,
      },
      success: function (res) {
        console.log(res.data)
        const payParams = res.data.payParams;
        that.wxPay(payParams);
      },
      fail: function (error) {
        console.error('Failed to get payment parameters:', error);
      }
    });
  }, 
  wxPay: function (payParams) {
    wx.requestPayment({
      timeStamp: payParams.timeStamp,
      nonceStr: payParams.nonceStr,
      package: payParams.package,
      signType: payParams.signType,
      paySign: payParams.paySign,
      success: function (res) {
        console.log('Payment success:', res);
        // 处理支付成功的逻辑
      },
      fail: function (error) {
        console.log(payParams)
        console.error('Payment failed:', error);
        // 处理支付失败的逻辑
      }
    });
  }

})